package com.example.base.utils.model;

import lombok.Data;

import java.util.*;

public class ListMapModel {

    private List<Map<String,Object>> mapList;

    private Map<String,Object> repalceMap;

    private Map<String,DefaultModel> defaultMap;

    private List<String> removeList;

    public ListMapModel(List<Map<String,Object>> mapList){
        this.mapList=mapList;
    }

    public ListMapModel replace(String oldField,String newField){
        if(repalceMap==null){
            repalceMap=new HashMap<>();
        }
        repalceMap.put(oldField,newField);
        return this;
    }

    public ListMapModel remove(String ... args){
        if(removeList==null){
            removeList=new ArrayList<>();
        }
        removeList.addAll(Arrays.asList(args));
        return this;
    }

    public ListMapModel remove(String field){
        if(removeList==null){
            removeList=new ArrayList<>();
        }
        removeList.add(field);
        return this;
    }

    public ListMapModel replace(String oldField,String newField,Object defalutValue){
        if(repalceMap==null){
            repalceMap=new HashMap<>();
        }
        ReplaceModel replaceModel=new ReplaceModel(newField,defalutValue);
        repalceMap.put(oldField,replaceModel);
        return this;
    }

    public ListMapModel addDefaultValue(String field,Object value){
        if(defaultMap==null){
            defaultMap=new HashMap<>();
        }
        DefaultModel defaultModel=new DefaultModel(value);
        defaultMap.put(field,defaultModel);
        return this;
    }

    public void execute(){
        if(mapList!=null){
            if(removeList!=null){
                mapList.forEach(e->{
                    removeList.forEach(e::remove);
                });
            }
            if(repalceMap!=null){
                mapList.forEach(e->{
                    repalceMap.forEach((k,v)->{
                        if(e.containsKey(k)){
                            Object val=e.get(k);
                            if(v instanceof ReplaceModel){
                                ReplaceModel replaceModel=(ReplaceModel) v;
                                if(val==null){
                                    e.put(replaceModel.newField,replaceModel.defaultValue);
                                }else {
                                    e.put(replaceModel.newField,val);
                                }
                            }else{
                                e.put((String) v,val);
                            }
                            e.remove(k,val);
                        }
                    });
                });
            }
            if(defaultMap!=null){
                mapList.forEach(e->{
                   defaultMap.forEach((k,v)->{
                       e.put(k,v.getDefaultValue());
                   });
                });
            }
        }
    }

    @Data
    class ReplaceModel {

        private String newField;

        private Object defaultValue;

        public ReplaceModel(String newField,Object defaultValue){
            this.newField=newField;
            this.defaultValue=defaultValue;
        }

    }

    @Data
    class DefaultModel{

        private Object defaultValue;

        public DefaultModel(Object defaultValue){
            this.defaultValue=defaultValue;
        }
    }


}
